package com.giantlizard.cloud.base.generics;

import com.giantlizard.cloud.base.enums.HttpStatusCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final int SUCCESS = 200;

    /** 失败 */
    public static final int FAIL = 500;

    private Integer code;
    private String msg;
    private T data;

    public static <T> R<T> ok(){
        R<T> r = new R<>();
        r.setCode(200);
        r.setMsg("success");
        return r;
    }
    public static <T> R<T> ok(T data){
        R<T> r = new R<>();
        r.setCode(200);
        r.setData(data);
        return r;
    }

    public static <T> R<T> ok(int code, String msg, T data){
        return  new R<>(code, msg,data);
    }

    public static <T> R<T> ok(int code, String msg){
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> ok(String msg,T data){
        R<T> r = new R<>();
        r.setCode(200);
        r.setMsg(msg);
        return r;
    }


    public static <T> R<T> error(String msg){
        R<T> r = new R<>();
        r.setCode(500);
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> error(int code, String msg){
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> error(HttpStatusCodeEnum enums){
        R<T> r = new R<>();
        r.setCode(enums.getCode());
        r.setMsg(enums.getMsg());
        return r;
    }

    public static <T> R<T> error(HttpStatusCodeEnum enums, String msg){
        R<T> r = new R<>();
        r.setCode(enums.getCode());
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> error(int code, String msg, T data){
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

}
